{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "00f1cc5d-85d6-4d3e-8046-96e9eaa2f301",
   "metadata": {},
   "source": [
    "# 1.NumPy"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "834bb7b1-1843-4745-a33e-c56fcf9c3af1",
   "metadata": {},
   "source": [
    "导入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "246f1b5b-b34f-48e3-a2cc-9971ff4ab50b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "1b717446-aae3-4591-a15b-c0d9ff6ca12c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.26.4'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看版本\n",
    "np.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc7052d3-f04d-4785-9126-3cb12fd4a808",
   "metadata": {},
   "source": [
    "## 1.创建ndarray\n",
    "\n",
    "### 1.使用np.array()由python list创建\n",
    "\n",
    "\n",
    "格式：``` n=np.array(list)```\n",
    "- numpy默认array()的所有元素的类型是相同的\n",
    "- 如果传进来的列表包含不同的类型，会将其转化为同一类型，优先级:str>float>int\n",
    "- array()常见类型\n",
    "- - int:int8、uint8、int16、int32、int64\n",
    "  - float:float16、float32、float64\n",
    "  - str：字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "c7bff18d-315e-47e9-bc5e-c8b76ddfa52d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['l', '2', '3', '4', '5'], dtype='<U11')"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l1=[1,2,3,4,5]\n",
    "n=np.array(l1)\n",
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8aa82853-0359-4642-a4f7-272b55d98aea",
   "metadata": {},
   "source": [
    "### 2.使用routines函数创建\n",
    "1 np.ones(shape,dtype=None,order='C')\n",
    "2 np.zeros(shape,dtype=None,order='C')\n",
    "3 np.full(shape,fill_value,dtype=None,order='C')\n",
    "\n",
    "参数说明：\n",
    "- shape:形状\n",
    "- fill_value：填充值\n",
    "- dtype=None:元素 默认为浮点数\n",
    "- order:{'C','F'},可选，C在内存中以行为主，F以列为主\n",
    "\n",
    "4 np.eye(N,M=None,k=0,dtype=float)\n",
    "参数说明：\n",
    "- N：行数\n",
    "- M：列数，默认为None，表示与行数一致\n",
    "- k=0：向右偏移0个位置\n",
    "\n",
    "5 np.linspace(start,stop,num=50,endpoint=True,retstep=False,dtype=None)\n",
    "参数说明：\n",
    "- strat:开始值\n",
    "- stop:结束值\n",
    "- num=50:等差数列的个数，默认50\n",
    "- endpoint:是否取到结束值\n",
    "- retstep=False:是否返回等差值步长\n",
    "\n",
    "6 np.arange([strat],stop,[step],dtype=None)\n",
    "参数说明：\n",
    "- strat:开始值(可选，默认0)\n",
    "- stop:结束值(不包含结束值)\n",
    "- step:步长(可选，默认1)\n",
    "\n",
    "7 np.random.randint(low,high=None,size=None,dtype='I')\n",
    "参数说明：\n",
    "- low:最小值\n",
    "- high=None:最大值\n",
    "- - high=None时，生成的数值在[0,low)区间内\n",
    "  - 如果high不为None时，生成的数值在[low,high)区间内\n",
    "- size=None:数组形状，默认输出一个随机值\n",
    "\n",
    "8 np.random.randn(d0,d1,...,dn)\n",
    "创建一个符合正态分布的多维数组\n",
    "> 标准正态分布又称u分布，是以0为均数、以1为标准差的正态分布，记为N(0,1)标准正态分布，在0左右出现的概率最大，越远出现概率越小\n",
    "\n",
    "参数说明：\n",
    "- dn:第n个维度的数值\n",
    "\n",
    "9 np.random.normal(loc=0.0,scale=1.0,size=None)\n",
    "创建一个符合正态分布的多维数组\n",
    "参数说明：\n",
    "- loc=0.0：均值，对应着正态分布中心\n",
    "- scala：标准差，对应分布的宽度，scale越大，正态分布的曲线越矮越胖，反之越高瘦\n",
    "- size=None:数组形状\n",
    "\n",
    "10 np.random.random(size=None)\n",
    "创建一个元素为[0,1)之间的随机数的多维数组\n",
    "\n",
    "11 np.random.rand(d0,d1,...,dn)\n",
    "创建一个元素为[0,1)的随机数的多维数组\n",
    "与np.random.random功能类似\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "ed00ba58-a93b-4124-b693-9e59c3970b74",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.91061733, 0.53325764, 0.857661  ],\n",
       "       [0.60990868, 0.77064915, 0.34904686]])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建三行五列默认元素为1.的列表\n",
    "n=np.ones((3,5))\n",
    "n\n",
    "# 创建三行五列默认元素为0的列表\n",
    "n=np.zeros((3,5))\n",
    "n\n",
    "# 创建三行五列默认元素为指点元素的列表\n",
    "n=np.full((3,5),fill_value=8)\n",
    "n\n",
    "# 创建主对角线全是1，其他位置全是0的二维数组\n",
    "n=np.eye(3,k=1)\n",
    "n\n",
    "# 创建1到100元素个数有20个的等差数列\n",
    "n=np.linspace(1,100,num=20,dtype=int)\n",
    "n\n",
    "\n",
    "# 创建一个五行十列的随机范围为[10,20)内的数列\n",
    "n=np.random.randint(low=10,high=20,size=(5,10))\n",
    "n\n",
    "\n",
    "#创建一个10行2列正态分布数组\n",
    "n=np.random.randn(10,2)\n",
    "n\n",
    "\n",
    "#创建一个2行3列均值为10,标准差为10的正态分布数组\n",
    "n=np.random.normal(loc=10,scale=10,size=(2,3))\n",
    "n\n",
    "\n",
    "# 创建一个2行3列在[0,1)之间的随机数的数组\n",
    "n=np.random.random((2,3))\n",
    "n\n",
    "# 创建一个2行3列在[0,1)之间的随机数的数组\n",
    "n=np.random.rand(2,3)\n",
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0c5662a-fb0d-4f9d-8a3c-658f81ebf0c0",
   "metadata": {},
   "source": [
    "## 2.Numpy属性\n",
    "\n",
    "- ndim:维度\n",
    "- shape:形状（各个维度的长度）\n",
    "- size:总长度\n",
    "- atype:元素类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "e927d167-5221-4d3c-8b86-c98e9142fc7f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 3)\n",
      "2\n",
      "6\n",
      "float64\n"
     ]
    }
   ],
   "source": [
    "print(n.shape)#(2, 3) 表示一维大小为2，二维为3\n",
    "print(n.ndim)#2  表示共有2个维度\n",
    "print(n.size)#6  表示总大小为6\n",
    "print(n.dtype)#float64  表示数组类型为float64"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c5b4eb2-1e69-4c4c-aba9-be78b4a1d01a",
   "metadata": {},
   "source": [
    "## 3.Numpy基本操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91a81b09-b3d4-4c8d-9b3f-4cf3d9e52b83",
   "metadata": {},
   "source": [
    "### 1.索引操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "id": "a3bc0bc7-8bee-41b8-bf59-df0fad412d58",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[6 7 5 6 7]\n",
      " [4 3 7 3 8]\n",
      " [8 8 1 4 9]\n",
      " [0 5 7 3 2]]\n",
      "2\n",
      "2\n",
      "100\n"
     ]
    }
   ],
   "source": [
    "# 一维操作\n",
    "L=[1,2,3,4,5,6,7]\n",
    "L[0],L[-1]\n",
    "\n",
    "#二维操作\n",
    "n=np.random.randint(0,10,size=(4,5))\n",
    "print(n)\n",
    "print(n[-1][-1])\n",
    "print(n[-1,-1])\n",
    "n[-1,-1]=100\n",
    "print(n[-1,-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 211,
   "id": "bd92c3bf-7a6e-448b-87b5-d8aacfca8ee1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6]\n",
      "[[12 82 47 26 68]\n",
      " [ 8 81 93 18 68]\n",
      " [18 15 19 54 92]\n",
      " [36  5 61 61 30]\n",
      " [38 49 79  0 77]]\n"
     ]
    }
   ],
   "source": [
    "### 2.切片操作\n",
    "#一维\n",
    "L1=[1,2,3,4,5,6]\n",
    "# 二维\n",
    "L2=np.random.randint(0,100,size=(5,5))\n",
    "L3=np.random.randint(0,100,size=(5,5,5))\n",
    "print(L1)\n",
    "print(L2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 215,
   "id": "4b8851d2-79a6-4a5c-bd96-52b7f93930a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 4, 5]\n",
      "[[93 18]\n",
      " [19 54]]\n",
      "===========\n",
      "[[93 18]\n",
      " [61 61]\n",
      " [79  0]]\n",
      "===========\n",
      "[[38 49 79  0 77]\n",
      " [36  5 61 61 30]\n",
      " [18 15 19 54 92]\n",
      " [ 8 81 93 18 68]\n",
      " [12 82 47 26 68]]\n",
      "===========\n",
      "[[68 26 47 82 12]\n",
      " [68 18 93 81  8]\n",
      " [92 54 19 15 18]\n",
      " [30 61 61  5 36]\n",
      " [77  0 79 49 38]]\n",
      "[[[58 51 78 49 20]\n",
      "  [34 98 70  2 25]\n",
      "  [10 93 49 96 92]\n",
      "  [58 78 31  4 35]\n",
      "  [ 0 69 71 27 65]]\n",
      "\n",
      " [[98 37 86 77 29]\n",
      "  [37 74 99  3 55]\n",
      "  [73 75 16  9 90]\n",
      "  [ 2 15 97 73 64]\n",
      "  [58 92 81 37 61]]\n",
      "\n",
      " [[90 22 29 11 83]\n",
      "  [64  1 22 25 30]\n",
      "  [23 90 26 50 12]\n",
      "  [73 51 58 89 66]\n",
      "  [20 16 37 31 21]]\n",
      "\n",
      " [[26 62 38 67 70]\n",
      "  [17 10 11 78 39]\n",
      "  [74 35 55 56 69]\n",
      "  [72 30  5 70 31]\n",
      "  [27  8 52 28 18]]\n",
      "\n",
      " [[59 40 64 99 32]\n",
      "  [76 89 72 42 73]\n",
      "  [69 78 28 53 95]\n",
      "  [43 56 79 34 63]\n",
      "  [85 32 44 91 48]]]\n"
     ]
    }
   ],
   "source": [
    "print(L1[1:5])\n",
    "\n",
    "print(L2[1:3,2:4])\n",
    "print('===========')\n",
    "print(L2[[1,3,4],2:4])\n",
    "print('===========')\n",
    "# 行翻转\n",
    "print(L2[::-1])\n",
    "print('===========')\n",
    "# 列翻转\n",
    "print(L2[:,::-1])\n",
    "# 第三维翻转\n",
    "print(L3[:,:,::-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f32efbed-9366-4fa0-9268-16d797907401",
   "metadata": {},
   "source": [
    "Ndarray\n",
    "- 变形：reshape\n",
    "- 级联(合并)：concatenate/np.hstack/np.vstack\n",
    "- 拆分：np.split/np.vsplit/np.hsplit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 225,
   "id": "e96e075c-9eaa-4114-8030-ad25a873840b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20]\n"
     ]
    }
   ],
   "source": [
    "# 一维数组\n",
    "n=np.arange(1,21)\n",
    "\n",
    "print(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 271,
   "id": "3de254a4-e6f8-41b2-a9f0-8378c83a261f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4  5]\n",
      " [ 6  7  8  9 10]\n",
      " [11 12 13 14 15]\n",
      " [16 17 18 19 20]]\n",
      "(4, 5)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,\n",
       "       18, 19, 20])"
      ]
     },
     "execution_count": 271,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 变形,rasheape:改变数组形状\n",
    "# 将一维改为四行五列的二维数组\n",
    "n=np.reshape(n,(4,5))\n",
    "print(n)\n",
    "print(n.shape)\n",
    "\n",
    "L2=n\n",
    "# 将二维转为一维\n",
    "\n",
    "L2=np.reshape(n,(20))\n",
    "L2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 275,
   "id": "05590c40-0328-4294-ba00-9bd0d5bbbcb8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[56 16 66  7 10]\n",
      " [96 41 84 23 30]\n",
      " [ 4 98 74  3  5]]\n",
      "[[ 4 41 43 45 54]\n",
      " [78 64 33 18 55]\n",
      " [41 67  1 37 41]]\n"
     ]
    }
   ],
   "source": [
    "# 定义数组\n",
    "n1=np.random.randint(0,100,size=(3,5))\n",
    "n2=np.random.randint(0,100,size=(3,5))\n",
    "\n",
    "print(n1)\n",
    "print(n2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 289,
   "id": "0ee38860-d740-4920-8c26-829a009c8754",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[56, 16, 66,  7, 10,  4, 41, 43, 45, 54],\n",
       "       [96, 41, 84, 23, 30, 78, 64, 33, 18, 55],\n",
       "       [ 4, 98, 74,  3,  5, 41, 67,  1, 37, 41]])"
      ]
     },
     "execution_count": 289,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 级联（合并）\n",
    "np.concatenate((n1,n2))#默认上下合并\n",
    "np.concatenate((n1,n2),axis=0)#上下合并axis=0,表示第一个维度(行)合并\n",
    "np.concatenate((n1,n2),axis=1)#左右合并axis=1,表示第二个维度(列)合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 295,
   "id": "81373c0d-c290-48d9-ac3b-4bcd8b65d746",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[56, 16, 66,  7, 10],\n",
       "       [96, 41, 84, 23, 30],\n",
       "       [ 4, 98, 74,  3,  5],\n",
       "       [ 4, 41, 43, 45, 54],\n",
       "       [78, 64, 33, 18, 55],\n",
       "       [41, 67,  1, 37, 41]])"
      ]
     },
     "execution_count": 295,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.hstack水平合并，左右合并\n",
    "np.hstack((n1,n2))\n",
    "# np.vstack垂直合并,上下合并\n",
    "np.vstack((n1,n2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b510413-ff3f-4a3d-98f1-24de0928124d",
   "metadata": {},
   "source": [
    "#### 拆分\n",
    "- np.split\n",
    "- np.vsplit\n",
    "- np.hsplit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 302,
   "id": "37caab60-6a30-4cf5-b191-e46d108fe7d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10, 42, 91, 99],\n",
       "       [93, 56, 32, 45],\n",
       "       [10, 65, 65, 99],\n",
       "       [18, 40, 89,  5],\n",
       "       [18, 48, 73, 77],\n",
       "       [74, 85, 19, 17]])"
      ]
     },
     "execution_count": 302,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n=np.random.randint(0,100,size=(6,4))\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 336,
   "id": "8ae13d4f-e9ac-40e5-83ff-3e0750c08df1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[10, 42],\n",
       "        [93, 56],\n",
       "        [10, 65],\n",
       "        [18, 40],\n",
       "        [18, 48],\n",
       "        [74, 85]]),\n",
       " array([[91, 99],\n",
       "        [32, 45],\n",
       "        [65, 99],\n",
       "        [89,  5],\n",
       "        [73, 77],\n",
       "        [19, 17]])]"
      ]
     },
     "execution_count": 336,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 垂直拆分：将数组垂直拆分为三份\n",
    "np.vsplit(n,1)\n",
    "np.vsplit(n,(1,2,4))\n",
    "\n",
    "# 水平拆分：将数组水平拆分为2份\n",
    "np.hsplit(n,2)\n",
    "np.hsplit(n,(1,2,4))\n",
    "\n",
    "# split拆分\n",
    "# 可以通过axis控制水平垂直拆分，0为行，1为列\n",
    "# 水平拆分\n",
    "np.split(n,2,axis=0)\n",
    "# 垂直拆分\n",
    "np.split(n,2,axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1b740ff-5362-460c-80dd-e288ea2d21c2",
   "metadata": {},
   "source": [
    "#### 数组的拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 343,
   "id": "abf8b8b0-67e9-4ee4-83c1-20dea28b35b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([100,   1,   2,   3,   4,   5,   6,   7,   8,   9])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([100,   1,   2,   3,   4,   5,   6,   7,   8,   9])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 数组赋值赋的是数组的地址不是该数组\n",
    "n=np.arange(10)\n",
    "n2=n\n",
    "n2[0]=100\n",
    "display(n,n2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 347,
   "id": "6187ec0a-c67e-4eb9-9e58-18385fb23117",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([100,   1,   2,   3,   4,   5,   6,   7,   8,   9])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "n1=np.arange(10)\n",
    "n2=n1.copy()\n",
    "n2[0]=100\n",
    "display(n1,n2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70548927-2cbb-496d-9688-665f7a94138c",
   "metadata": {},
   "source": [
    "## 3.NumPy聚合操作\n",
    "- np.sum:求和\n",
    "- np.min:最小值\n",
    "- np.max:最大值\n",
    "- np.mean:平均值\n",
    "- np.average:平均值\n",
    "- np.median:中位数\n",
    "- np.percentile:百分位数\n",
    "- np.argmin:最小值对应的下标\n",
    "- np.argmax:最大值对应的下标\n",
    "- np.std:标准差\n",
    "- np.var:方差\n",
    "- np.power:次方，求幂\n",
    "- np.argwhere:按条件查找"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "020ce7ce-79a3-4002-bbd9-9c3aa147fad7",
   "metadata": {},
   "source": [
    "### 1.求和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 355,
   "id": "9a8c868a-94c0-4168-9e93-457cc3780323",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 355,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n=np.arange(10)\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 357,
   "id": "9f376b6e-6035-46ab-a4df-6ea0c601dc30",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "45"
      ]
     },
     "execution_count": 357,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 363,
   "id": "6d12ba42-80a2-4c52-b9d7-d841edb4aee8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 5, 9, 1, 8],\n",
       "       [7, 4, 9, 8, 1],\n",
       "       [1, 4, 9, 6, 7]])"
      ]
     },
     "execution_count": 363,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n=np.random.randint(0,10,size=(3,5))\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 369,
   "id": "beec35b7-de1a-452c-a301-8e61f446d3f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([32, 29, 27])"
      ]
     },
     "execution_count": 369,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 所有数的和\n",
    "np.sum(n)\n",
    "\n",
    "# 将每个列求和\n",
    "np.sum(n,axis=0)\n",
    "# 将每行求和\n",
    "np.sum(n,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 406,
   "id": "ca3fc6e1-a8d4-4bad-8432-3d912bd59c79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([9, 5, 9, 1, 8, 7, 4, 9, 8, 1, 1, 4, 9, 6, 7])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "8.648888888888887"
      ]
     },
     "execution_count": 406,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求最大值\n",
    "np.max(n)\n",
    "# 求每个列的最小值\n",
    "np.min(n,axis=0)\n",
    "\n",
    "# 求平均值\n",
    "np.mean(n)\n",
    "np.average(n)\n",
    "\n",
    "# 求中位数\n",
    "np.median(n)\n",
    "\n",
    "np.percentile(n,q=50)#百分位数，q=50表示中位数\n",
    "\n",
    "n=n.reshape(-1)\n",
    "display(n)\n",
    "# 求最大值和最小值的索引下标\n",
    "np.argmax(n)\n",
    "np.argmin(n)\n",
    "\n",
    "np.argwhere(n==np.max(n))#按条件找到所有最大值的下标\n",
    "\n",
    "# 次方\n",
    "np.power(n,3)\n",
    "n**3\n",
    "\n",
    "np.std(n)#标准差\n",
    "np.var(n)#方差"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bb9857d-eec2-4ad8-abf7-f0efc9711222",
   "metadata": {},
   "source": [
    "### np.sum和np.nansum\n",
    "- nan:数值类型，not a number:不是一个正常的数值，表示空\n",
    "- np.nan:float类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 411,
   "id": "596d12e4-f05a-439c-8a6a-134dd99b8c94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.,  2.,  3., nan])"
      ]
     },
     "execution_count": 411,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n=np.array([1,2,3,np.nan])\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 413,
   "id": "6381d282-31f8-4179-af2e-5b6e610c5865",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "nan"
      ]
     },
     "execution_count": 413,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 415,
   "id": "c2b57d40-26fa-4aae-855a-29fb7194cc06",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6.0"
      ]
     },
     "execution_count": 415,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.nansum(n)#排除掉nan之后，剩下数的和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 419,
   "id": "f098505f-852a-4170-b7cc-1cc71930c0d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 4. 矩阵的操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec9d443d-b883-48ac-a93e-37b57f967678",
   "metadata": {},
   "source": [
    "### 1.基本矩阵操作\n",
    "1 算术运算符\n",
    "- 加减乘除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 452,
   "id": "81cfc9b0-152c-457f-8bc5-5f4903e315a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 6, 1, 3, 7],\n",
       "       [0, 4, 3, 9, 8],\n",
       "       [4, 9, 9, 2, 3],\n",
       "       [1, 2, 1, 4, 9]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[9, 3, 6, 5, 3],\n",
       "       [3, 5, 2, 3, 5],\n",
       "       [4, 5, 9, 4, 4],\n",
       "       [2, 7, 4, 6, 6],\n",
       "       [0, 9, 0, 7, 2]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[7, 4, 8, 4, 8],\n",
       "       [6, 1, 2, 5, 8],\n",
       "       [9, 3, 8, 5, 3],\n",
       "       [8, 4, 9, 2, 7],\n",
       "       [1, 8, 0, 8, 1]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "n=np.random.randint(0,10,(4,5))\n",
    "n1=np.random.randint(0,10,(5,5))\n",
    "n2=np.random.randint(0,10,(5,5))\n",
    "display(n,n1,n2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 438,
   "id": "91a56b77-a876-4079-8823-923543c12147",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 1, 1, 1],\n",
       "       [0, 1, 1, 1, 0],\n",
       "       [0, 0, 0, 0, 1],\n",
       "       [1, 1, 0, 0, 1]], dtype=int32)"
      ]
     },
     "execution_count": 438,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 加法\n",
    "n+10\n",
    "#减法\n",
    "n-10\n",
    "#乘法\n",
    "n*10\n",
    "#除法\n",
    "n/10\n",
    "# 整除\n",
    "n//2\n",
    "# 次方\n",
    "n**2\n",
    "# 取模\n",
    "n%2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 446,
   "id": "6bdb2fff-f9bc-49b0-9992-1a8e174878ba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[28, 15,  6, 14,  2],\n",
       "       [15, 12,  0, 35, 12],\n",
       "       [42, 42,  7, 21,  0],\n",
       "       [20,  0, 28,  0,  0]])"
      ]
     },
     "execution_count": 446,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1+n2#矩阵之间相加\n",
    "n1-n2#矩阵之间相减\n",
    "n1*n2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 454,
   "id": "63c2ea68-7e17-4ede-8ef5-caa0599aafba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[178, 101, 171, 115, 152],\n",
       "       [ 98,  75,  77,  93,  96],\n",
       "       [175,  96, 150, 126, 131],\n",
       "       [146,  99, 116, 123, 132],\n",
       "       [112,  53,  81,  75, 123]])"
      ]
     },
     "execution_count": 454,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 矩阵积(乘)\n",
    "# 矩阵积条件\n",
    "# 第一个矩阵的列数,要等于第二矩阵的行数\n",
    "np.dot(n1,n2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 464,
   "id": "9990a312-3586-4ad7-898a-37c10d3441dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 464,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 矩阵逆\n",
    "n=np.array([[1,2,3],[2,5,4],[4,5,8]])\n",
    "\n",
    "# 矩阵的逆\n",
    "np.linalg.inv(n)\n",
    "\n",
    "# 矩阵的行列式\n",
    "np.round(np.linalg.det(n))\n",
    "\n",
    "# 矩阵的秩(满秩矩阵,奇异矩阵)\n",
    "# 方阵\n",
    "np.linalg.matrix_rank(n)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d85e75a-f9e6-4f00-a198-5c5bcad49616",
   "metadata": {},
   "source": [
    "### 广播机制\n",
    "ndarray广播机制的两条规则\n",
    "- 规则1:为缺失的维度补维度\n",
    "- 规则2:缺失元素用已有值填充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 493,
   "id": "304828be-1181-4d54-af64-95efc09146b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [1, 1, 1]], dtype=int8)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2], dtype=int8)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "m=np.ones((2,3),dtype=np.int8)\n",
    "b=np.ones((2,3),dtype=np.int8)\n",
    "a=np.arange(3,dtype=np.int8)\n",
    "display(m,a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 495,
   "id": "98dc3a7b-6490-47a0-9d5c-ba54cbe079d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 2, 2],\n",
       "       [2, 2, 2]], dtype=int8)"
      ]
     },
     "execution_count": 495,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a+m\n",
    "m+b\n",
    "# 自动将缺失的内容用上一行内容复制下来"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50012920-b930-4265-9e11-e573eae940fa",
   "metadata": {},
   "source": [
    "### 其他常用数学操作\n",
    "- abs:绝对值\n",
    "- sqrt:开平方\n",
    "- square:平方\n",
    "- exp:以e为底的指数\n",
    "- log:自然对数(以1为底的对数)\n",
    "- sin:正弦\n",
    "- cos:余弦\n",
    "- tan:正切\n",
    "- round:四舍五入\n",
    "- ceil:向上取整\n",
    "- floor:向下取整\n",
    "- cumsum:累加运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 498,
   "id": "aeaa77c4-d73a-4c20-b185-9c0c79ec07b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "n= np.array([1,4,8,9,16,25,64])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 511,
   "id": "dde3b612-3f92-4ed1-991a-9a0b6c85d3f4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1,   5,  13,  22,  38,  63, 127])"
      ]
     },
     "execution_count": 511,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.abs(n)#绝对值\n",
    "np.sqrt(n)#开平方\n",
    "np.square(n)#平方\n",
    "np.exp(n)#以e为底的指数\n",
    "np.log(n)#自然对数\n",
    "np.sin(n)#正弦\n",
    "np.cos(n)#余弦\n",
    "np.tan(n)#正切\n",
    "np.round(n)#四舍五入\n",
    "np.ceil(n)#向上取整\n",
    "np.floor(n)#向下取整\n",
    "np.cumsum(n)#累加运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aeeb0729-59f3-4c0f-a856-a1857fb55a6c",
   "metadata": {},
   "source": [
    "### 数组排序\n",
    "- np.sort():不改变原数组\n",
    "- ndarray.sort():改变原数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 515,
   "id": "685fc191-8007-4eab-a9be-ca32942abe63",
   "metadata": {},
   "outputs": [],
   "source": [
    "n=np.array([1,412,231,22,3,2,44])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 519,
   "id": "a2383f1e-baa5-4086-9041-2f7d441e311a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1,   2,   3,  22,  44, 231, 412])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([  1, 412, 231,  22,   3,   2,  44])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a=np.sort(n)\n",
    "display(a,n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 521,
   "id": "adb77181-f4aa-45d7-a936-efedc0bac612",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1,   2,   3,  22,  44, 231, 412])"
      ]
     },
     "execution_count": 521,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n.sort()\n",
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85d667dd-bb6f-4e78-9cee-5ab4414270d6",
   "metadata": {},
   "source": [
    "### 文件操作\n",
    "- save:保存ndarray到一个npy文件\n",
    "- savez:将多个array保存到一个npz文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 535,
   "id": "dd0e5b76-51bd-4d0a-b5ec-b20693a29243",
   "metadata": {},
   "outputs": [],
   "source": [
    "x=np.arange(5)\n",
    "y=np.arange(10,20)\n",
    "\n",
    "#save:保存为x.npy文件\n",
    "np.save('x',x)\n",
    "\n",
    "#savez:保存为arr.npz文件\n",
    "np.savez('arr.npz',xarr=x,yarr=y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b84f98e-dbe8-4c94-a8a2-352a4dbcfeac",
   "metadata": {},
   "source": [
    "**读取数据**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 544,
   "id": "15fea39f-0a37-4dd3-acc4-e521ca443513",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4])"
      ]
     },
     "execution_count": 544,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#读取npy文件\n",
    "np.load('x.npy')\n",
    "\n",
    "#读取npz文件\n",
    "np.load('arr.npz')['yarr']\n",
    "np.load('arr.npz')['xarr']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "346daef4-76e0-44b8-8bb4-72e10635d5e4",
   "metadata": {},
   "source": [
    "**csv,txt文件读写操作**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 547,
   "id": "37288283-27bb-48a8-af70-9954ec46714e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 7, 5, 1],\n",
       "       [0, 2, 6, 7],\n",
       "       [7, 5, 1, 6]])"
      ]
     },
     "execution_count": 547,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n=np.random.randint(0,10,size=(3,4))\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 555,
   "id": "5d51de84-5853-427e-ad56-ca4503b00c49",
   "metadata": {},
   "outputs": [],
   "source": [
    "#存储到csv或txt\n",
    "#delimiter修改分隔符字符,默认为空格,\n",
    "#注意:csv分隔符为','在保存是需要修改\n",
    "np.savetxt('arr.csv',n,delimiter=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 561,
   "id": "72e8ec2c-4da9-4ae4-a3fd-4eceb842dd19",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3., 7., 5., 1.],\n",
       "       [0., 2., 6., 7.],\n",
       "       [7., 5., 1., 6.]])"
      ]
     },
     "execution_count": 561,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#读取csv或txt\n",
    "np.loadtxt('arr.csv',delimiter=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "faa83dac-5bbe-4750-ac64-5fbe409f878b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
